﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Xml.Linq;

namespace WEI_Share.Helpers
{
	public sealed class Conversion
	{
		#region Constructor
        /// <summary>
        /// Private constructor
        /// </summary>
        private Conversion()
        {
        }
        #endregion


        #region ToString(decimal?...)
        /// <summary>
        /// Converts a decimal to a formatted string
        /// </summary>
        /// <param name="toConvert"></param>
        /// <returns></returns>
        public static string ToString(decimal? toConvert, string formatToUse)
        {
            if (toConvert.HasValue)
                return toConvert.Value.ToString(formatToUse);

            return string.Empty;
        }
        #endregion


        #region ToString(int?...)

        /// <summary>
        /// Converts a int to a formatted string
        /// </summary>
        public static string ToString(int? toConvert)
        {
            return ToString(toConvert, String.Empty);
        }

        public static string ToString(int? toConvert, string formatToUse)
        {
            return ToString(toConvert, formatToUse, String.Empty);
        }

        public static string ToString(int? toConvert, string formatToUse, string defaultValue)
        {
            if (toConvert.HasValue)
                return toConvert.Value.ToString(formatToUse);

            return defaultValue;
        }

        #endregion


        #region ToString(datetime?...)

        /// <summary>
        /// Converts a int to a formatted string
        /// </summary>
        public static string ToString(DateTime? toConvert)
        {
            return ToString(toConvert, String.Empty);
        }

        public static string ToString(DateTime? toConvert, string formatToUse)
        {
            return ToString(toConvert, formatToUse, String.Empty);
        }

        public static string ToString(DateTime? toConvert, string formatToUse, string defaultValue)
        {
            if (toConvert.HasValue)
                return toConvert.Value.ToString(formatToUse);

            return defaultValue;
        }

        #endregion

        
        #region ToDecimal(string)
        /// <summary>
        /// Converts a string to decimal or 0 if can't convert
        /// </summary>
        /// <param name="toConvert"></param>
        /// <returns></returns>
        public static decimal ToDecimal(string toConvert)
        {
            bool isSuccessful = false;
            decimal? tempValue = ToDecimalOrNull(toConvert, out isSuccessful);
            if (isSuccessful)
                return tempValue.Value;
            return 0;
        }
        #endregion


        #region ToDecimalOrNull(string)
        /// <summary>
        /// Converts a string to decimal or null if empty or can't convert
        /// </summary>
        /// <param name="toConvert"></param>
        /// <returns></returns>
        public static decimal? ToDecimalOrNull(string toConvert)
        {
            bool isSuccessful = false;
            return ToDecimalOrNull(toConvert, out isSuccessful);
        }
        #endregion


        #region ToDecimalOrNull(string, out bool)
        /// <summary>
        /// Converts a string to decimal or null if empty or can't convert
        /// </summary>
        /// <param name="toConvert"></param>
        /// <param name="wasSuccessful"></param>
        /// <returns></returns>
        public static decimal? ToDecimalOrNull(string toConvert, out bool isSuccessful)
        {
            isSuccessful = false;

            if (!string.IsNullOrEmpty(toConvert))
            {
                decimal tempValue = 0;
                if (decimal.TryParse(toConvert, out tempValue))
                {
                    isSuccessful = true;
                    return tempValue;
                }
            }
            return null;
        }
        #endregion


        #region ToInt(string)
        /// <summary>
        /// Converts a string to int or 0 if can't convert
        /// </summary>
        /// <param name="toConvert"></param>
        /// <returns></returns>
        public static int ToInt(string toConvert)
        {
            bool isSuccessful = false;
            int? tempValue = ToIntOrNull(toConvert, out isSuccessful);
            if (isSuccessful)
                return tempValue.Value;
            return 0;
        }
        #endregion


        #region ToInt(string, int)
        /// <summary>
        /// Converts a string to int or the specified default value if can't convert
        /// </summary>
        /// <param name="toConvert"></param>
        /// <returns></returns>
        public static int ToInt(string toConvert, int defaultValue)
        {
            bool isSuccessful = false;
            int? tempValue = ToIntOrNull(toConvert, out isSuccessful);
            if (isSuccessful)
                return tempValue.Value;

            return defaultValue;
        }
        #endregion


        #region ToIntOrNull(string)
        /// <summary>
        /// Converts a string to int or null if empty or can't convert
        /// </summary>
        /// <param name="toConvert"></param>
        /// <returns></returns>
        public static int? ToIntOrNull(string toConvert)
        {
            bool isSuccessful = false;
            return ToIntOrNull(toConvert, out isSuccessful);
        }
        #endregion


        #region ToIntOrNull(string, out bool)
        /// <summary>
        /// Converts a string to decimal or null if empty or can't convert
        /// </summary>
        /// <param name="toConvert"></param>
        /// <param name="wasSuccessful"></param>
        /// <returns></returns>
        public static int? ToIntOrNull(string toConvert, out bool isSuccessful)
        {
            isSuccessful = false;

            if (!string.IsNullOrEmpty(toConvert))
            {
                int tempValue = 0;
                if (int.TryParse(toConvert, out tempValue))
                {
                    isSuccessful = true;
                    return tempValue;
                }
            }
            return null;
        }
        #endregion


        #region ToDateOrNull(string)
        public static DateTime? ToDateOrNull(string toConvert)
        {
            bool isSuccessful = false;
            return ToDateOrNull(toConvert, out isSuccessful);
        }
        #endregion


        #region ToDateOrNull(string, out bool)
        public static DateTime? ToDateOrNull(string toConvert, out bool isSuccessful)
        {
            isSuccessful = false;

            if (!string.IsNullOrEmpty(toConvert))
            {
                DateTime tempValue = DateTime.MinValue;
                if (DateTime.TryParse(toConvert, out tempValue))
                {
                    isSuccessful = true;
                    return tempValue;
                }
            }
            return null;
        }
        #endregion


        #region ToDate(string)
        /// <summary>
        /// Converts a string to DateTime or DateTime.Min if can't convert
        /// </summary>
        /// <param name="toConvert"></param>
        /// <returns></returns>
        public static DateTime ToDate(string toConvert)
        {
            bool isSuccessful = false;
            DateTime? tempValue = ToDateOrNull(toConvert, out isSuccessful);
            if (isSuccessful)
                return tempValue.Value;
            return DateTime.MinValue;
        }
        #endregion


        #region ToDateOrNull(string, datetime)

        public static DateTime? ToDateOrNull(string time, DateTime date)
        {
            DateTime myTime = DateTime.MinValue;
            if (DateTime.TryParse(time, out myTime))
                return new DateTime(date.Year, date.Month, date.Day, myTime.Hour, myTime.Minute, myTime.Second);
            else
                return null;
        }

        #endregion


        #region ToBool(string, out bool)

        public static bool ToBool(string toConvert, out bool returnValue)
        {
            returnValue = false;
            bool isSuccessful = false;

            bool? m_value = ToBoolOrNull(toConvert);
            if (m_value.HasValue)
            {
                isSuccessful = true;
                returnValue = m_value.Value;
            }

            return isSuccessful;
        }

        #endregion


        #region ToBool(string)
        /// <summary>
        /// converts a value to a bool or false if unable to 
        /// </summary>
        /// <param name="toConvert"></param>
        /// <param name="returnValue"></param>
        /// <returns></returns>
        public static bool ToBool(string toConvert)
        {
            return ToBool(toConvert, false);
        }

        #endregion


        #region ToBool(string)
        /// <summary>
        /// converts a value to a bool or the specified default if unable to 
        /// </summary>
        /// <param name="toConvert"></param>
        /// <param name="returnValue"></param>
        /// <returns></returns>
        public static bool ToBool(string toConvert, bool defaultValue)
        {
            bool? m_value = ToBoolOrNull(toConvert);
            if (m_value.HasValue)
                return m_value.Value;

            return defaultValue;
        }

        #endregion


        #region ToGuidOrNull(string)
        /// <summary>
        /// converts a value to a guid or null if unable to 
        /// </summary>
        /// <param name="toConvert"></param>
        /// <returns></returns>
        public static Guid? ToGuidOrNull(string toConvert)
        {
            try
            {
                System.Guid convertedValue = new Guid(toConvert);
                return convertedValue;
            }
            catch
            {
                //
            }

            return null;
        }
        #endregion


        #region ToGuidOrNull(object,...)
        /// <summary>
        /// Checks the value in the specific column passed in (dr["title"]), if dbnull returns null
        /// </summary>
        /// <param name="toConvert"></param>
        /// <returns></returns>
        public static System.Guid? ToGuidOrNull(object toConvert)
        {
            if (toConvert != null)
            {
                return ToGuidOrNull(toConvert.ToString());
            }

            return null;
        }
        #endregion


		#region ToGuid(string)
		/// <summary>
		/// converts a value to a guid or the supplied default if unable to 
		/// </summary>
		/// <param name="toConvert"></param>
		/// <param name="defaultValue"></param>
		/// <returns></returns>
		public static Guid ToGuid(string toConvert)
		{
			return ToGuid(toConvert, Guid.Empty);
		}
		#endregion


        #region ToGuid(string)
        /// <summary>
        /// converts a value to a guid or the supplied default if unable to 
        /// </summary>
        /// <param name="toConvert"></param>
        /// <param name="defaultValue"></param>
        /// <returns></returns>
        public static Guid ToGuid(string toConvert, System.Guid defaultValue)
        {
            System.Guid? convertedValue = ToGuidOrNull(toConvert);
            if (convertedValue.HasValue)
                return convertedValue.Value;

            return defaultValue;
        }
        #endregion


        #region ToBoolOrNull(string)

        public static bool? ToBoolOrNull(string toConvert)
        {
            bool? returnValue = null;
            if (!String.IsNullOrEmpty(toConvert))
            {
                string value = toConvert.ToLower();
                switch (value)
                {
                    case "true":
                    case "1":
                        returnValue = true;
                        break;

                    case "false":
                    case "0":
                        returnValue = false;
                        break;

                    default:
                        returnValue = null;
                        break;
                }
            }

            return returnValue;
        }

        #endregion




        #region ToLong(string)
        /// <summary>
        /// Converts a string to long or 0 if can't convert
        /// </summary>
        /// <param name="toConvert"></param>
        /// <returns></returns>
        public static long ToLong(string toConvert)
        {
            bool isSuccessful = false;
            long? tempValue = ToLongOrNull(toConvert, out isSuccessful);
            if (isSuccessful)
                return tempValue.Value;
            return 0;
        }
        #endregion


        #region ToLong(string, int)
        /// <summary>
        /// Converts a string to long or the specified default value if can't convert
        /// </summary>
        /// <param name="toConvert"></param>
        /// <returns></returns>
        public static long ToLong(string toConvert, long defaultValue)
        {
            bool isSuccessful = false;
            long? tempValue = ToLongOrNull(toConvert, out isSuccessful);
            if (isSuccessful)
                return tempValue.Value;

            return defaultValue;
        }
        #endregion


        #region ToLongOrNull(string)
        /// <summary>
        /// Converts a string to int or null if empty or can't convert
        /// </summary>
        /// <param name="toConvert"></param>
        /// <returns></returns>
        public static long? ToLongOrNull(string toConvert)
        {
            bool isSuccessful = false;
            return ToLongOrNull(toConvert, out isSuccessful);
        }
        #endregion


        #region ToLongOrNull(string, out bool)
        /// <summary>
        /// Converts a string to decimal or null if empty or can't convert
        /// </summary>
        /// <param name="toConvert"></param>
        /// <param name="wasSuccessful"></param>
        /// <returns></returns>
        public static long? ToLongOrNull(string toConvert, out bool isSuccessful)
        {
            isSuccessful = false;

            if (!string.IsNullOrEmpty(toConvert))
            {
                long tempValue = 0;
                if (long.TryParse(toConvert, out tempValue))
                {
                    isSuccessful = true;
                    return tempValue;
                }
            }
            return null;
        }
        #endregion
    }
}